/**
 * 分页显示数据表格，具有分页显示数据、排序、设置分页大小功能
 */
Ext.namespace("Ext.ux.grid");

Ext.ux.grid.czStateGrid = Ext.extend(Ext.grid.GridPanel, {

	// 表格结构
	structure: null,
	// 关键字段
	keyField: null,
	keyFieldIndex: -1,
	enableRefreshBtn: true,	//刷新按钮
	browseUrl: '',				//浏览地址URL
	exportExcel: true,	//导出
    
    iconScale: 'medium',
    
	// 获取数据的URL
	url: '',
	
	// 默认排序字段
	defaultSortField: '',
	defaultSortDir: 'DESC',
	
	// 预加载数据
	preload: true,
	
	// 是否需要分页工具栏，默认为true
	needPage: true,
	
	// 是否显示分页信息
	showPageInfo: true,
	
	// 每页记录数
	pageSize: 10,
	
	// 是否显示加载提示
	loadMask: true,
	
	// 是否显示边框
	frame: false,

	stripeRows: true,
	columnLines: true,
	
	groupByCombo: null,

	view: new Ext.grid.GroupingView({
		forceFit: true,
		groupTextTpl: '{text} ({[values.rs.length]} {["项"]})'
	}),
  
	// 组件初始化
	initComponent: function(){
		if(this.structure != null){
			this.initStructure();	// 根据表格结构构造ColumnModel对象和Record对象
		}
		Ext.ux.grid.czStateGrid.superclass.initComponent.call(this);
	},

	// 根据表格结构构造ColumnModel对象、Record对象、Store对象等
	initStructure: function(){

		this.tbar = new Ext.Toolbar();
		this.tbar.addItem('-');
		
		if(this.enableRefreshBtn){
			this.tbar.addItem({
				iconCls: 'icon-refresh',
				tooltip: '刷新',
				handler: function(){
					this.store.reload();
				},
				scope: this
			});
			this.tbar.addItem('-');
		}

		if(this.exportExcel){
			this.tbar.addItem({
				text: '导出',
				iconCls: 'icon-take-back',
				scale: this.iconScale,
				handler: function(){
					this.exportData();
				},
				scope: this
			});
			this.tbar.addItem('-');
			this.on('rowdblclick', function(grid){
			this.editData();
		    }, this);
		}

		this.sm = new Ext.grid.RowSelectionModel({singleSelect:false});

		var groupByComboData = new Array();

		var oColModel = new Array();
		var oRecord = new Array();

		oColModel[0] = new Ext.grid.RowNumberer();

		// 构造GRID所需对象
		for(var i=0, len=this.structure.length; i<len; i++){

			// 表格结构的列定义
			var column = this.structure[i];

			// 如果字段为hidden，则不生成columnMode
			//if (column.hidden == undefined || !column.hidden){
				
				if(column.isGroupField){
					var groupByComboItem = [column.name, column.header];
					groupByComboData[groupByComboData.length] = groupByComboItem;
				}
				
				oColModel[oColModel.length] = {
					header: column.header,
					dataIndex: column.name,
					hidden: column.hidden == null ? false : column.hidden,
					width: column.width == null ? 50 : column.width,
					sortable: column.sortable
				};
				
				if(column.renderer != null)
					oColModel[oColModel.length-1].renderer = column.renderer;
				else if(column.type == 'date')
					oColModel[oColModel.length-1].renderer = Ext.util.Format.dateRenderer('Y-m-d');
				else if(column.type == 'datetime')
					oColModel[oColModel.length-1].renderer = Ext.util.Format.dateRenderer('Y-m-d H:i:s');
				else if(column.type == 'task-sign-icon')
					oColModel[oColModel.length-1].renderer = taskSignIcon;
				else if(column.type == 'agent-icon')
					oColModel[oColModel.length-1].renderer = agentIcon;
				else if(column.type == 'proc-progress-bar')
					oColModel[oColModel.length-1].renderer = procProgressBar;
				else if(column.type == 'task-progress-bar')
					oColModel[oColModel.length-1].renderer = taskProgressBar;
				else
					oColModel[oColModel.length-1].renderer = Ext.grid.ColumnModel.defaultRenderer;
			//}

			oRecord[oRecord.length] = {
				name: column.name
			};
		}
		
		
		// 构造columnModel
		this.cm = new Ext.grid.ColumnModel(oColModel);
		this.colModel = this.cm;
	
		// 默认可排序
		this.cm.defaultSortable = true;

		// 构造记录对象和reader对象
		var record = Ext.data.Record.create(oRecord);
		var reader;
		
		if(this.keyField != null){
			reader = new Ext.data.JsonReader({
				totalProperty: 'totalCount',
				root: 'result',
				id: this.keyField
			}, record);
		}
		else if(this.keyFieldIndex >= 0){
			reader = new Ext.data.ArrayReader({
				totalProperty: 'totalCount',
				root: 'result',
				idIndex: this.keyFieldIndex
			}, record);
		}
		
		// 构造store对象
		this.ds = new Ext.data.GroupingStore({
			proxy: new Ext.data.HttpProxy({
				url: ctx + this.url
			}),
			reader: reader,
			sortInfo: {
				field: this.defaultSortField,
				direction: this.defaultSortDir
			},
			groupField: null,
			remoteGroup: true,
			remoteSort: true
		});
		this.store = this.ds;
		
		// 添加分页工具栏  
		if (this.needPage){
			var pagingToolbar = new Ext.PagingToolbar({
				store: this.ds,
				pageSize: this.pageSize,
				displayInfo: this.showPageInfo,
				displayMsg: '第 {0} - {1} 条记录 / &nbsp;共 {2} 条记录',
				emptyMsg: '<font color="red">提示：没有任何符合条件的记录！</font>'
			});
			this.bbar = pagingToolbar;
			this.bottomToolbar = this.bbar;
		}

		// 加载数据列表
		if(this.preload)
			this.store.load({params:{start:0, limit:this.pageSize}});

		this.tbar.addItem('->');
		this.tbar.addItem('-')
		this.tbar.addItem(' ');


		var groupByComboStore = new Ext.data.ArrayStore({
	        fields: ['name', 'header'],
	        data: groupByComboData
	    });
		
		
		this.tbar.addItem('-');
	},
	
	//编辑主要是修改证书有效期
	editData:function(){
		window.editData();
	},
	
	//导出
	exportData: function(){
		window.exportData();
	},
	//浏览更多
	browse: function(){
		parent.window.location = ctx + this.browseUrl;
	}
});